<template>
  <view class="goods-item">
    <!-- 左侧 -->
    <view class="goods-item-left">
      <!-- 是否选中，这个组件的显示是通过父组件进行传值决定的 -->
      <radio :value="goods.goods_state" :checked="goods.goods_state" v-if="showRadio"  color="#C00000" @click="changeState"/>
      <!-- 如果本身没有图片地址就用默认的 -->
      <image :src="goods.goods_small_logo || defaultImgSrc" class="goods-pic"></image>
    </view>
    <!-- 右侧 -->
    <view class="goods-item-right">
      <view class="item-title">
        {{ goods.goods_name }}
      </view>
      <view class="item-info">
        <view class="item-info-price">
          ￥{{ goods.goods_price | toFixed }}
        </view>
        <uni-number-box :min="1" :max="9999" :value="goods.goods_count" @change="changeCount" @blur="checkLegality" v-if="showNumberBox" />
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    props: {
      // 商品的信息对象
      goods: {
        type: Object,
        default: {}
      },
      showRadio:{
        type: Boolean,
        default: false
      },
      showNumberBox: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        defaultImgSrc: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png', // 如果goods中没有图片则默认为这个图片
        originCount: 0, // 上一次修改数量前的值
      };
    },
    watch: {
      goods: {
        // 解构出 goods 下的 goods_count
        handler({ goods_count: originVal }) {
          this.originCount = originVal
        },
        immediate: true,
        deep: true, // 监听goods 下面的 goods_count
      }
    },
    methods: {
      changeState() {
        // 触发父元素绑定的事件，并且返回修改的goods中的goods_id和最新goods_state
        this.$emit('changeState', {
          goods_id: this.goods.goods_id,
          goods_state: !this.goods.goods_state, // 商品最新勾选状态
        })
      },
      /**
       * @param {Object} val 数量最新的值
       */
      changeCount(val) {
        this.$emit('changeNum', {
          goods_id: this.goods.goods_id, 
          goods_count: +val, // +val 等价于 Number(val) 转换为数值型
        })
      },
      /**
       * 验证输入是否合法
       * @param {Object} e
       */
      checkLegality(e) {
        let val = e.detail.value
        if(!/^\d{1,}$/.test(val)) {
          // console.log(this.originCount) // 这是一个整型数据
          // 验证失败，强制会原来的值
          e.detail.value = this.originCount
        }
      }
    },
    filters: {
      toFixed(num) {
        return Number(num).toFixed(2)
      }
    }
  }
</script>

<style lang="scss">
  .goods-item {
    display: flex;
    padding: 10px 0;
    border-bottom: 1px solid #f0f0f0;
      
    .goods-item-left {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-right: 5px;
      
      .goods-pic {
        display: block;
        width: 100px;
        height: 100px;
      }
    }
    
    .goods-item-right {
      display: flex;
      flex: 1; // 占满盒子空间
      flex-direction: column;
      justify-content: space-between;
      
      .item-title {
        font-size: 13px;
      }
      
      .item-info {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .item-info-price {
          font-size: 16px;
          color: #C00000;
        }
      }
    }
  }
</style>
